package com.example.my_objectives.model;

import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

/**
 * @author mohamed
 *
 */
public class Common {
	public final String		NULL	= "NULL";
	private final String		LOG	= Common.class.getName();

	private SQLiteDatabase	_database;

	public SQLiteDatabase get_database() {
		return _database;
	}

	public void set_database(SQLiteDatabase _database) {
		this._database = _database;
	}

	/**
	 * Constructor
	 *
	 * @param db
	 *           the opened database
	 */
	public Common(SQLiteDatabase db) {
		this._database = db;
	}

	/**
	 * deletes a record from a table
	 *
	 * @param tableName
	 *           the name of the table to delete from
	 * @param id
	 *           id of the record to delete
	 * @return null if no exception raised, the exception otherwise
	 */
	protected Exception delete(TableName tableName, long id) {
		Exception returnValue = null;
		try {
			String query = "DELETE FROM " + tableName + " WHERE id = " + id;
			Log.d(LOG, query);
			_database.execSQL(query);
		}// try
		catch (Exception ex) {
			returnValue = ex;
			Log.e(LOG, ex.getMessage());
		}// catch
		return returnValue;
	}

	/**
	 * Format a Boolean to SQLite integer
	 *
	 * @param value
	 * @return 0 = false, 1 = true
	 */
	public int format(Boolean value) {
		return (value ? 1 : 0);
	}

}
